New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support output.libraryTarget: 'module' #2933

Open
donaldpipowitch opened this Issue Aug 30, 2016 · 23 comments

Comments

Projects
None yet
@donaldpipowitch

donaldpipowitch commented Aug 30, 2016

I'm submitting a feature request

It would be nice, if output.libraryTarget could support the ES2015 module format.

What is the motivation / use case for changing the behavior?

Say I develop a fancy component called fancy-component with webpack. I use a very custom config with specific loader settings e.g. for inlining images and stuff like that. My module exports FancyComponent and BigFancyComponent.

A friend develops a single page application and uses rollup to do that. He knows nothing about webpack, but wants to use my FancyComponent, but not BigFancyComponent. He installs fancy-component and imports FancyComponent. BigFancyComponent should not be included in the build (because of tree shaking).

As far as I know this is currently not possible. My friend would need fancy-component in a format using ES2015 modules. But currently these include webpack specific loader logic. I really need to publish fancy-component without webpack specific logic, but with ES2015 modules.

@TheLarkInn

This comment has been minimized.

Member

TheLarkInn commented Aug 30, 2016

I definitely see the merit in this. @sokra I don't think we have a way already to export vanilla es2015 / es modules. I think that maybe this can be part of the rollup story (module combining/(and this splitting exporting)).

@donaldpipowitch

This comment has been minimized.

donaldpipowitch commented Oct 13, 2016

Is there anything I can do to help with that? Can I tackle this problem myself as someone who is unfamiliar with webpacks code base? Or is this a rabbit hole into the guts of webpack? :)

@TheLarkInn

This comment has been minimized.

Member

TheLarkInn commented Oct 13, 2016

For a feature like this we should write up the design for the feature. Questions I think should be answerered first:

  • if someone uses CJS/AMD/non-esm dependencies in their library, how does webpack convert this into esm modules?
  • Should externals be leveraged for lib dependencies?
@TheLarkInn

This comment has been minimized.

Member

TheLarkInn commented Oct 13, 2016

Once we have the behavior designed then you can go straight into adding the test cases.

@donaldpipowitch

This comment has been minimized.

donaldpipowitch commented Oct 13, 2016

Seems to be like big step. Could we introduce some workaround in the meantime? Like emitting plain non-bundled commonjs modules without __webpack_require__, webpackBootstrap, etc.?

Use case is similar. I want to build a project with webpacks powerful loaders and I want to output some sort of modules without webpack specific logic, so that they can be consumed more easily by third party build tools. (Or webpack itself, too! E.g. I can't consume CommonJS modules outputted from webpack right now and use process.env. A good example: https://github.com/facebook/react is created as CommonJS and they use ``process.env.NODE_ENV. Their "main"` file points to the CommonJS entry. They can only do that, because they don't use webpack in the first place. If they would want to include a loader somewhere, they currently couldn't emit a target like they have right now which doesn't force others to use webpack, too.)

@donaldpipowitch

This comment has been minimized.

donaldpipowitch commented Oct 14, 2016

Turns out... it looks like I already can output CommonJS modules and keep process.env. (From my knowledge using CommonJS - or ES2015 modules in the future - and allowing environment variables is the lowest common denominator between current popular bundlers. Other features like loaders are webpack-specific.)

You need to set output.libraryTarget: 'commonjs2' (I guess most webpack users know this) and you need to set something like new DefinePlugin({ 'process.env.FOO': 'process.env.FOO' }) (that was new to me). So it should be possible to create a lib with webpack which can still be consumed by other bundlers without loosing the feature of setting environment variables.

The only feature which isn't supported in that way is tree shaking, which brings me back to the original feature request.

@nadavsinai

This comment has been minimized.

nadavsinai commented Jan 19, 2017

@donaldpipowitch thanks for bringing this up, very useful indeed, I would love to contribute too, I am going to try to learn the current code base first...

@AnimaMundi

This comment has been minimized.

AnimaMundi commented May 10, 2017

Would love to this implemented!

@HipsterZipster

This comment has been minimized.

HipsterZipster commented Sep 27, 2017

Have there been any updates to this issue in the past year? Is it being tracked elsewhere?

@yansern

This comment has been minimized.

yansern commented Sep 28, 2017

Having to learn both webpack and rollup is no fun! Would love to have this implemented in webpack.

@madou

This comment has been minimized.

madou commented Oct 15, 2017

@sokra @TheLarkInn hey guys. if one (me) wanted to start looking at how to resolve this issue would you have any suggestions? any ideas of how much would need to change to support this?

i'd be keen to lend some time if you could put me on the right path :-)

saki7 added a commit to saki7/nagato that referenced this issue Oct 29, 2017

Enable ES2015's (un-transpiled) module imports while supporting Webpa…
…ck's tree shaking.

This is currently *not* supported by Webpack (see webpack/webpack#1979 (comment) and webpack/webpack#2933 for rationale).

We workaround this by exposing our babel config to the wild (i.e. js/nagato/babel-options.js). This way we can give full controls for both bundling (i.e. 'tree shaking' in Webpack) and module building (i.e. 'transpiling') to the library users (I hope).

We just can't enforce our users to use some huge-sized arbitrary bundle.

Users with ES2015+ (or whatever) environment shall `require()` this config file inside their 'webpack.config.js', and use it as a hash object for the `babel-loader`'s `option: ` option. This could be achieved by looking into the `module: ` option in `package.json`; which (I believe) is the default behavior for Webpack when you use the native `import Something from 'other-external-library'` syntax.

If this is not desirable, use the fully-transpiled .js file inside our distributed npm package. This could be achieved by referring to the old-school `"main"` value inside the package.json.

Disclaimer:
By using this method we abandon Webpack-specific aggresive transpilation features for our entire library. This means we can't use Webpack-specific custom `import`s (i.e. importing non-JavaScript files like images (.png, .jpg, etc.) inside our library (.js)).

Additional notes:
This issue described in the disclaimer section can be workarounded by specifying your library-specific `npm run build` action inside the `prepare` section of package.json.
@lastmjs

This comment has been minimized.

lastmjs commented Mar 19, 2018

It seems to me like Webpack is going to need functionality for compiling CommonJS and other formats to ES Modules, am I correct? I've been diving deep into this for the past few days. It seems like the whole world has traditionally been compiling from other formats to CommonJS. There is a popular babel plugin that does this. That problem seems to have been solved quite well by the community. But we need to go the other way, from CommonJS (or other formats) to ES Modules.

I'm not sure how much compilation/transpilation Webpack does itself, but that might be outside of its scope. Perhaps a Babel plugin would be the best choice here. Then the functionality could be leveraged by all libraries that need to go from CommonJS -> ES Modules. There is some prior art:

Basic Babel plugin for going from CommonJS -> ES Modules (basic, I've already run into a few blocking bugs, no community): https://www.npmjs.com/package/babel-plugin-transform-commonjs-es2015-modules

Advanced Rollup plugin for going from CommonJS -> ES Modules (seems very popular, most likely works very well, not very portable outside of Rollup. Might be easy to integrate within Webpack if Webpack has Rollup integration): https://github.com/rollup/rollup-plugin-commonjs

As I see it, this functionality should be created independent of Webpack and then incorporated as a dependency, through either a Bable plugin (most ideal), or perhaps a Rollup plugin (already implemented, might need to finagle).

Disclaimer, I'm not a heavy Webpack user nor in the Webpack community, these are just my thoughts as I've been trying to tackle the issue of CommonJS -> ES Modules

@ooflorent

This comment has been minimized.

Member

ooflorent commented Mar 19, 2018

I think webpack must wait until CJS / ESM interop has been standarized into node. There are plenty projects that made opinionated choices about it and it could break the existing codebases if things got spec-ed differently.

@moroine

This comment has been minimized.

moroine commented Mar 20, 2018

Hello,

I have a library fully written in Es6, and due to webpack limitations I'm not able to propose my library in es6 module that would enable treeshaking feature on for clients

@ooflorent

This comment has been minimized.

Member

ooflorent commented Mar 20, 2018

@moroine For library, I would recommend using rollup. And webpack for applications.
Rich Harris (rollup's creator) wrote about it: Webpack and Rollup: the same but different.

@blikblum

This comment has been minimized.

blikblum commented Mar 20, 2018

It seems to me like Webpack is going to need functionality for compiling CommonJS and other formats to ES Modules

I would restrict exporting to ES module (output.libraryTarget: 'module') to libraries written with ES modules only

@bsmith-cycorp

This comment has been minimized.

bsmith-cycorp commented Jun 19, 2018

Important use case for this: ES6 modules are now supported natively, but many packages in NPM still use CommonJS export syntax, which means they can't be imported by the browser directly. A strategy I was going to use was to bundle all my node modules up with webpack - eliminating all the disparate export syntaxes - and then just follow the ES6 syntax in my own code. This is apparently impossible, and I don't understand why.

Someone above mentioned something called rollup. Sounds like I may be dropping webpack altogether.

@mnpenner

This comment has been minimized.

Contributor

mnpenner commented Jun 20, 2018

@bsmith-cycorp For libraries, Rollup does seem like our only option right now. That's what I did. Wasn't eager to learn new bundler, but it wasn't too hard.

@yairopro

This comment has been minimized.

yairopro commented Aug 23, 2018

Here's a workaround I managed to config:

const Path = require('path');
const path = Path.resolve(__dirname, 'dist');
const EditableSourcesWebpackPlugin = require('editable-sources-webpack-plugin');

{
	entry: "./src/index.js",
	output: {
		filename: "module.js",
		path,
		library: 'x',
		libraryTarget : "window",
	},

	plugins : [
		// export default the compiled module
		new EditableSourcesWebpackPlugin(
			/^module\.js$/,
			sourceCode => ("export default " + sourceCode.slice('window["x"] ='.length)),
		)
	],

	// disable uglify
	optimization: {
		minimizer: []
	},
}

It first export as a global variable window.x, and then change start of the output source code window["x"] = into export default.

I use this in my package require.web.

@eatsjobs

This comment has been minimized.

eatsjobs commented Oct 2, 2018

Is there any news about this?

@zhangenming

This comment has been minimized.

zhangenming commented Dec 7, 2018

Is there any news about this??

@evilebottnawi

This comment has been minimized.

Member

evilebottnawi commented Dec 7, 2018

Feel free to send a PR

@ascoders

This comment has been minimized.

ascoders commented Dec 10, 2018

Need this feature to publish component with blob url replace web worker require, when using worker-loader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment