Skip to content

Commit

Permalink
refactor: droped webpack@4 (#58)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: yes
  • Loading branch information
cap-Bernardito committed Jan 11, 2021
1 parent b82ab5a commit 9176392
Show file tree
Hide file tree
Showing 16 changed files with 2,069 additions and 3,769 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10.x, 12.x, 14.x]
webpack-version: [4, latest]
webpack-version: [latest]

runs-on: ${{ matrix.os }}

Expand Down
16 changes: 1 addition & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ myFunction('Hello world');
import {
myVariable,
myFunction,
} from 'exports-loader?exports[]=myVariable&exports[]=myFunction!./file.js';
} from 'exports-loader?exports=myVariable,myFunction!./file.js';
// Adds the following code to the file's source:
//
// ...
Expand All @@ -74,19 +74,6 @@ console.log(newVariable);
myFunction('Hello world');
```

```js
import { file } from 'exports-loader?[name]!./file.js';
// Adds the following code to the file's source:
//
// ...
// Code
// ...
//
// export { file };

file('string');
```

```js
const {
myFunction,
Expand Down Expand Up @@ -284,7 +271,6 @@ Examples:
- `[single Foo]` - generates `module.exports = Foo;`.
- `[multiple Foo]` - generates `module.exports = { Foo };`.
- `[multiple Foo FooA]` - generates `module.exports = { 'FooA': Foo };`.
- `[[name]]` - generates ES module named exports and exports a variable equal to the filename, for `single.js` it will be `single`, generates `export { single };`.
- `[named [name] [name]Alias]` - generates ES module named exports and exports a value equal to the filename under other name., for `single.js` it will be `single` and `singleAlias`, generates `export { single as singleAlias };`.

> ⚠ You need to set `type: "commonjs"` to use `single` or `multiple` syntaxes.
Expand Down
5,140 changes: 2,010 additions & 3,130 deletions package-lock.json

Large diffs are not rendered by default.

32 changes: 15 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,35 @@
"dist"
],
"peerDependencies": {
"webpack": "^4.0.0 || ^5.0.0"
"webpack": "^5.0.0"
},
"dependencies": {
"schema-utils": "^3.0.0",
"loader-utils": "^2.0.0",
"source-map": "^0.6.1"
},
"devDependencies": {
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"@babel/preset-env": "^7.11.5",
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.5.2",
"babel-loader": "^8.1.0",
"cross-env": "^7.0.2",
"babel-jest": "^26.6.3",
"babel-loader": "^8.2.2",
"cross-env": "^7.0.3",
"del": "^6.0.0",
"del-cli": "^3.0.1",
"eslint": "^7.10.0",
"eslint-config-prettier": "^6.12.0",
"eslint": "^7.17.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-import": "^2.22.1",
"husky": "^4.3.0",
"jest": "^26.5.2",
"lint-staged": "^10.4.0",
"husky": "^4.3.7",
"jest": "^26.6.3",
"lint-staged": "^10.5.3",
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"standard-version": "^9.0.0",
"webpack": "^4.44.2"
"prettier": "^2.2.1",
"standard-version": "^9.1.0",
"webpack": "^5.12.3"
},
"keywords": [
"webpack"
Expand Down
10 changes: 1 addition & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import { getOptions } from 'loader-utils';
import { validate } from 'schema-utils';
import { SourceMapConsumer, SourceNode } from 'source-map';

import schema from './options.json';
Expand All @@ -12,13 +10,7 @@ import { getExports, renderExports } from './utils';
const FOOTER = '/*** EXPORTS FROM exports-loader ***/\n';

export default function loader(content, sourceMap) {
const options = getOptions(this);

validate(schema, options, {
name: 'Exports Loader',
baseDataPath: 'options',
});

const options = this.getOptions(schema);
const type = options.type || 'module';
const callback = this.async();

Expand Down
5 changes: 5 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"title": "Exports Loader options",
"definitions": {
"ExportItemString": {
"type": "string",
Expand Down Expand Up @@ -41,6 +42,10 @@
},
"exports": {
"anyOf": [
{
"type": "string",
"minLength": 1
},
{
"$ref": "#/definitions/ExportItem"
},
Expand Down
24 changes: 11 additions & 13 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { interpolateName } from 'loader-utils';

function forError(item) {
return typeof item === 'string'
? item
Expand Down Expand Up @@ -115,11 +113,15 @@ function getIdentifiers(array) {

function getExports(type, exports) {
let result;
const exportItems =
typeof exports === 'string' && exports.includes(',')
? exports.split(',')
: exports;

if (Array.isArray(exports)) {
result = exports.map((item) => resolveExports(type, item));
if (Array.isArray(exportItems)) {
result = exportItems.map((item) => resolveExports(type, item));
} else {
result = [resolveExports(type, exports)];
result = [resolveExports(type, exportItems)];
}

const hasMultipleDefault = result.filter(
Expand Down Expand Up @@ -181,9 +183,7 @@ function renderExports(loaderContext, type, exports) {
break;
}

const name = interpolateName(loaderContext, defaultExport[0].name, {});

code += `${name};\n`;
code += `${defaultExport[0].name};\n`;
}

if (namedExports.length > 0) {
Expand All @@ -199,11 +199,9 @@ function renderExports(loaderContext, type, exports) {

namedExports.forEach((namedExport, i) => {
const needComma = i < namedExports.length - 1;
const name = interpolateName(loaderContext, namedExport.name, {});
const alias = namedExport.alias
? interpolateName(loaderContext, namedExport.alias, {})
: // eslint-disable-next-line no-undefined
undefined;
const { name } = namedExport;
// eslint-disable-next-line no-undefined
const alias = namedExport.alias || undefined;

code += ` ${
type === 'commonjs'
Expand Down
Loading

0 comments on commit 9176392

Please sign in to comment.