Skip to content

Commit

Permalink
fix: ts check
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Jan 20, 2021
1 parent de9b8da commit 169ca4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = {
],
rules: {
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/prefer-regexp-exec': 'off',
'comma-dangle': 'off',
'no-eval': 'off'
},
env: {
jest: true,
Expand Down
42 changes: 20 additions & 22 deletions src/style-loader.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import {styleStore} from './styles-store';

import type { loader } from 'webpack';
import type {loader} from 'webpack';
import ___CSS_LOADER_GET_URL_IMPORT___ from 'css-loader/dist/runtime/getUrl';

interface CssRes {
exports: {
[id: number]: string[];
locals: Record<string, string>;
};
};

export default function (this: loader.LoaderContext, content: string) {
const loaderContext = this;
const callback = loaderContext.async();
const callback = this.async();

checkIsAfterCssLoader(loaderContext);
checkIsAfterCssLoader(this);

extractCssResult(content, loaderContext, (cssRes) => {
extractCssResult(content, this, cssRes => {
styleStore.set(this.resourcePath, {
locals: cssRes.exports.locals,
cssCode: cssRes.exports[0][1]
cssCode: cssRes.exports[0][1],
});

callback && callback(undefined, content);
Expand Down Expand Up @@ -43,25 +49,24 @@ function checkIsAfterCssLoader(loaderContext: loader.LoaderContext) {
function extractCssResult(content: string, loaderContext: loader.LoaderContext, callback: (res: CssRes) => void) {
const fileMap = {} as Record<string, string>;
const m = content.match(/require\(['"](.*)['"]\)/g)?.map(item => {
const r = item.match(/require\(['"](.*)['"]\)/)
const r = item.match(/require\(['"](.*)['"]\)/);
return r && r[1];
}) || [];

const pArr = [] as Promise<unknown>[];
const pArr = [] as Array<Promise<unknown>>;

m.forEach(req => {
if (!req || /.js$/.test(req)) {
return;
}

const p = new Promise((resolve, reject) => {
const p = new Promise<void>((resolve, reject) => {
loaderContext.loadModule(req, (err, source) => {
if (err) {
reject(err);
return;
}

const __webpack_public_path__ = loaderContext._compiler.options.output?.publicPath || '';
let path = '';

// 这里严重依赖 file-loader 的输出格式:
Expand All @@ -70,11 +75,11 @@ function extractCssResult(content: string, loaderContext: loader.LoaderContext,

fileMap[req] = path;
resolve();
})
});
});

pArr.push(p);
})
});

Promise.all(pArr)
.then(() => {
Expand All @@ -84,21 +89,14 @@ function extractCssResult(content: string, loaderContext: loader.LoaderContext,
if (/css\-loader\/dist\/runtime\/getUrl.js/.test(req)) {
return ___CSS_LOADER_GET_URL_IMPORT___;
}

if (fileMap[req]) {
return fileMap[req];
}

return () => [];
});

callback(res);
});
}

interface CssRes {
exports: {
[id: number]: string[];
locals: Record<string, string>;
}
};

0 comments on commit 169ca4c

Please sign in to comment.