Skip to content

Commit

Permalink
change package.json type to module
Browse files Browse the repository at this point in the history
  • Loading branch information
shian15810 committed Nov 1, 2020
1 parent bcd3c6f commit ab12f5c
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*
!index.cjs
!index.js
!index.mjs
66 changes: 66 additions & 0 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

const path = require('path');
const InertEntryPlugin = require('inert-entry-webpack-plugin');
const loaderUtils = require('loader-utils');
const { SingleEntryPlugin } = require('webpack');

module.exports = function () {};

module.exports.pitch = function (request) {
const callback = this.async();

const options = loaderUtils.getOptions(this) || {};
const context = options.context || this.rootContext;
const name = options.name || path.basename(this.resourcePath);
const filename = loaderUtils.interpolateName(this, name, {
context: context,
});
const outputDir = options.path || '.';
const plugins = options.plugins || [];
if (options.inert) {
plugins.unshift(new InertEntryPlugin());
}

// name of the entry and compiler (in logs)
const debugName = loaderUtils.interpolateName(this, '[name]', {});

// create a child compiler (hacky)
const compiler = this._compilation.createChildCompiler(
debugName,
{ filename: filename },
plugins,
);
new SingleEntryPlugin(this.context, '!!' + request, debugName).apply(
compiler,
);

// add a dependency on the entry point of the child compiler, so watch mode works
this.addDependency(request);

// like compiler.runAsChild(), but remaps paths if necessary
// https://github.com/webpack/webpack/blob/f6e366b4be1cfe2770251a890d93081824789209/lib/Compiler.js#L206
compiler.compile(
function (err, compilation) {
if (err) return callback(err);

this.parentCompilation.children.push(compilation);
for (const name of Object.keys(compilation.assets)) {
this.parentCompilation.assets[path.join(outputDir, name)] =
compilation.assets[name];
}

// the first file in the first chunk of the first (should only be one) entry point is the real file
// see https://github.com/webpack/webpack/blob/f6e366b4be1cfe2770251a890d93081824789209/lib/Compiler.js#L215
const outputFilename = compilation.entrypoints.values().next().value
.chunks[0].files[0];

callback(
null,
'module.exports = __webpack_public_path__ + ' +
JSON.stringify(path.join(outputDir, outputFilename)) +
';',
);
}.bind(compiler),
);
};
68 changes: 2 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,2 @@
'use strict';

const path = require('path');
const InertEntryPlugin = require('inert-entry-webpack-plugin');
const loaderUtils = require('loader-utils');
const { SingleEntryPlugin } = require('webpack');

module.exports = function () {};

module.exports.pitch = function (request) {
const callback = this.async();

const options = loaderUtils.getOptions(this) || {};
const context = options.context || this.rootContext;
const name = options.name || path.basename(this.resourcePath);
const filename = loaderUtils.interpolateName(this, name, {
context: context,
});
const outputDir = options.path || '.';
const plugins = options.plugins || [];
if (options.inert) {
plugins.unshift(new InertEntryPlugin());
}

// name of the entry and compiler (in logs)
const debugName = loaderUtils.interpolateName(this, '[name]', {});

// create a child compiler (hacky)
const compiler = this._compilation.createChildCompiler(
debugName,
{ filename: filename },
plugins,
);
new SingleEntryPlugin(this.context, '!!' + request, debugName).apply(
compiler,
);

// add a dependency on the entry point of the child compiler, so watch mode works
this.addDependency(request);

// like compiler.runAsChild(), but remaps paths if necessary
// https://github.com/webpack/webpack/blob/f6e366b4be1cfe2770251a890d93081824789209/lib/Compiler.js#L206
compiler.compile(
function (err, compilation) {
if (err) return callback(err);

this.parentCompilation.children.push(compilation);
for (const name of Object.keys(compilation.assets)) {
this.parentCompilation.assets[path.join(outputDir, name)] =
compilation.assets[name];
}

// the first file in the first chunk of the first (should only be one) entry point is the real file
// see https://github.com/webpack/webpack/blob/f6e366b4be1cfe2770251a890d93081824789209/lib/Compiler.js#L215
const outputFilename = compilation.entrypoints.values().next().value
.chunks[0].files[0];

callback(
null,
'module.exports = __webpack_public_path__ + ' +
JSON.stringify(path.join(outputDir, outputFilename)) +
';',
);
}.bind(compiler),
);
};
export * from './index.cjs';
export { default } from './index.cjs';
2 changes: 0 additions & 2 deletions index.mjs

This file was deleted.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"type": "commonjs",
"type": "module",
"name": "entry-chunk-loader",
"version": "0.2.2",
"description": "Webpack loader to generate a new entry chunk.",
"exports": {
"import": "./index.mjs",
"require": "./index.js"
"import": "./index.js",
"require": "./index.cjs"
},
"main": "./index.js",
"main": "./index.cjs",
"scripts": {
"format": "prettier --write .",
"test": "ava test/index.mjs"
"test": "ava test/index.js"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion test/index.mjs → test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('basic usage', async (t) => {
{
test: /\.entry\.js$/,
use: {
loader: join(__dirname, '../index.js'),
loader: join(__dirname, '../index.cjs'),
options: { name: '[name].chunk.js' },
},
},
Expand Down
2 changes: 1 addition & 1 deletion test/src/child.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import 'file-loader?name=hi.jpeg!./hi.jpeg';
import '../../index.js?path=subchild&name=subchild.js!./child2.js';
import '../../index.cjs?path=subchild&name=subchild.js!./child2.js';
10 changes: 5 additions & 5 deletions test/src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '../../index.js?path=children&name=child1.js!./child.js';
import '../../index.js?inert&path=inertOut!./manifest.notjson';
import '../../index.js?inert&name=[name]-inert.[hash:6].js!./defaults';
import '../../index.js?name=[name].[hash:6].js!./defaults';
import '../../index.js!./defaults';
import '../../index.cjs?path=children&name=child1.js!./child.js';
import '../../index.cjs?inert&path=inertOut!./manifest.notjson';
import '../../index.cjs?inert&name=[name]-inert.[hash:6].js!./defaults';
import '../../index.cjs?name=[name].[hash:6].js!./defaults';
import '../../index.cjs!./defaults';
import './other.entry.js';

0 comments on commit ab12f5c

Please sign in to comment.